package engine;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import utilities.ReaderTools;

public class RelevanceFeedback {

    private IndexReader indexReader;
    private ArrayList<ResultEntry> results;
    private Double[] vector;
    private HashMap<String, Double> initialRequest;
    private HashMap<String, Double> newRequest;
    private String tfIdfIndexPath;
    
    public RelevanceFeedback(HashMap<String, Double> initialRequest, ArrayList<ResultEntry> results, IndexReader indexReader, String tfIdfIndexPath) {
        this.initialRequest = initialRequest;
        this.results = results;
        this.indexReader = indexReader;
        this.tfIdfIndexPath = tfIdfIndexPath;
    }

    public ArrayList<String> bestResultFromFirstRequest() {
        ArrayList<String> docChoosen = new ArrayList<String>();
        for (int i = 0; i < results.size(); i++) {
            ResultEntry resultEntry = results.get(i);
            if (resultEntry.getScore() > 0.001) {
                //System.out.println("Document " + resultEntry.getFilePath() + " pris");
                try {
                    docChoosen.add(indexReader.getDocId(resultEntry.getFilePath()));
                } catch (IOException ex) {
                    Logger.getLogger(RelevanceFeedback.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return docChoosen;
    }

    public HashMap<String, Double> makeNewRequest() {
        ArrayList<String> docChoosen = bestResultFromFirstRequest();
        this.newRequest = new HashMap<String, Double>(initialRequest);
        InputStream ips;
        try {
            ips = new FileInputStream(tfIdfIndexPath);


            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader br = new BufferedReader(ipsr);
            String line;
            try {
                //Lecture des lignes
                while ((line = br.readLine()) != null) {
                    String[] elt = new String[2];
                    elt = line.split(":");

                    String[] arrayElt = elt[1].split(";");

                    if (this.vector == null) {
                        vector = new Double[arrayElt.length];
                        
                        for (int i = 0; i < arrayElt.length; i++) vector[i] = 0.;
                    }
                    for (int i = 0; i < arrayElt.length; i++) {
                        if (docChoosen.contains(elt[0])) {
                            vector[i] += (Double.parseDouble((arrayElt[i]))) / (2.00);
                        } else {
                            vector[i] -= (Double.parseDouble((arrayElt[i]))) / (4.00);
                        }

                    }

                }
            } catch (IOException ex) {
                Logger.getLogger(ReaderTools.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ReaderTools.class.getName()).log(Level.SEVERE, null, ex);
        }

        int i = 0;
        for (Map.Entry<String, Double> entry : initialRequest.entrySet()) {
            this.newRequest.put(entry.getKey(), entry.getValue() + vector[i]);
            i++;
        }
        return this.newRequest;
    }
}
